package com.woke.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bean.MsContactType;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.woke.adapter.MsContactTypeViewProvider;
import com.woke.adapter.OnItemClickListener;
import com.zhongcai.online.R;

import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;


/**
 * 作者 ： 暗网
 * 创建时间：2017/5/9
 * 备注： 民生 联系人类型
 */

public class MsContactTypeDialogFragment extends AppCompatDialogFragment {


    private OnItemClickListener mItemClickListener;
    private MultiTypeAdapter mMulitypeAdapter;
    private Items mItems = new Items();

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }


    public void setItems(List<MsContactType> list) {
        if (list != null && list.size() > 0) {
            mItems.clear();
            mItems.addAll(list);
            if (mMulitypeAdapter != null) {
                mMulitypeAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
    }

    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        Context context = dialog.getContext();
        View rootView = LayoutInflater.from(context)
                .inflate(R.layout.activity_base_list, null);
        initToolbar(rootView);
        initRecyclerView(rootView);
        dialog.setContentView(rootView);
    }

    private void initRecyclerView(View rootView) {
        XRecyclerView xRecyclerView = (XRecyclerView) rootView.findViewById(R.id.xRecyclerView);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setLoadingMoreEnabled(false);
        xRecyclerView.setPullRefreshEnabled(false);

//        xRecyclerView.setHasFixedSize(true);
//        xRecyclerView.setNestedScrollingEnabled(false);

        mMulitypeAdapter = new MultiTypeAdapter();
        mMulitypeAdapter.register(MsContactType.class, new MsContactTypeViewProvider(mItemClickListener));
        xRecyclerView.setAdapter(mMulitypeAdapter);

        mMulitypeAdapter.setItems(mItems);
        mMulitypeAdapter.notifyDataSetChanged();


    }

    private void initToolbar(View rootView) {
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView titleTv = (TextView) rootView.findViewById(R.id.toolbar_title);
        titleTv.setText(R.string.merchant_contcat_type);

    }

}
